package com.car.frame.baidu.geocoding.api.method;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.car.frame.baidu.geocoding.api.GeocodingAPI;
import com.car.frame.baidu.geocoding.api.Gps;
import com.car.frame.baidu.geocoding.api.PositionUtil;
import com.car.frame.baidu.geocoding.api.coordtype;
import com.czr.frame.beans.Sjdw;
import com.czr.frame.beans.Zbzh;
import com.czr.frame.util.dao.GenericDAO;

/**
 * wgs84 转换成 gcj 坐标保存到数据 库
 * @author chenzhirong
 *
 */
public class Wgs2Gcj {
	
	private int zhly;
	
	public void setZhly(int zhly) {
		this.zhly = zhly;
	}

	private GeocodingAPI geocodingAPI;
	
	private static final Logger log = Logger.getLogger(Wgs2Gcj.class);
	
	private GenericDAO dao;
	
	public void setGeocodingAPI(GeocodingAPI geocodingAPI) {
		this.geocodingAPI = geocodingAPI;
	}

	public void setDao(GenericDAO dao) {
		this.dao = dao;
	}

	public GeocodingAPI getGeocodingAPI() {
		return geocodingAPI;
	}

	public GenericDAO getDao() {
		return dao;
	}

	/**
	 * 原坐标转换成GCJ坐标
	 */
	public void execute(){
		Logger.getLogger(Wgs2Gcj.class);
		List<Object> objects = dao.queryHql("from Sjdw");
		
		List<Sjdw> sjdws = new ArrayList<Sjdw>();
		for (Object object : objects) {
			if(object instanceof Sjdw){
				sjdws.add((Sjdw)object);
			}
		}
		
		log.info("开始"+sjdws.size()+"条记录");
		for (Sjdw sjdw : sjdws) {
			
			if(sjdw.getLat() != null && sjdw.getLng() != null){
				Gps gps = du2ejz(sjdw.getLat().trim(),sjdw.getLng().trim());
				Gps gcj02 = PositionUtil.gps84_To_Gcj02(gps.getWgLat(), gps.getWgLon());  
				
				//更新坐标信息
				String zb = sjdw.getLat().trim()+","+sjdw.getLng().trim();
				String path = geocodingAPI.getPath(zb, coordtype.wgs84ll);
				if(path != null){
					sjdw.setPath(path);
					String gcjZb = gcj02.getWgLat()+","+gcj02.getWgLon();
					String gcjPath = geocodingAPI.getPath(gcjZb, coordtype.gcj02ll);
					sjdw.setGcj02Path(gcjPath);
					sjdw.setGcj02Lat(String.valueOf(gcj02.getWgLat()));
					sjdw.setGcj02Lng(String.valueOf(gcj02.getWgLon()));
					dao.update(sjdw);
					log.info(String.format("原始地址：%s  详情：%s 转换成  GCJ02地址：%s 详情：%s,",gps.getWgLat()+","+gps.getWgLon(),path,gcj02,gcjPath));
				}
			}
		}
	}
	
	/**
	 * 计算两个误差
	 */
	public void execute2(){
		Logger.getLogger(Wgs2Gcj.class);
		List<Object> bbjects = dao.findAll(Sjdw.class);
		
		List<Sjdw> sjdws = new ArrayList<Sjdw>();
		for (Object object : bbjects) {
			if(object instanceof Sjdw){
				sjdws.add((Sjdw)object);
			}
		}
		
		log.info("开始"+sjdws.size()+"条记录");
		for (Sjdw sjdw : sjdws) {
			
			if(sjdw.getLat() != null && sjdw.getLng() != null && sjdw.getYysLat()!= null && sjdw.getYysLng()!= null){
				//运营商坐标
				Gps yysZb = new Gps(Double.valueOf(sjdw.getYysLat().trim()),Double.valueOf(sjdw.getYysLng().trim()));
				String yysZb2 = sjdw.getYysLat().trim()+","+sjdw.getYysLng().trim(); //用于获取地址path
				//运营商转换成  GPS 坐标
				Gps yysGpsZb = PositionUtil.gcj_To_Gps84(yysZb.getWgLat(), yysZb.getWgLon());  
				String yysGpsZb2 = String.valueOf(yysGpsZb.getWgLat()).trim()+","+String.valueOf(yysGpsZb.getWgLon()).trim(); 
				
				//更新坐标信息
				Gps gps = du2ejz(sjdw.getLat().trim(),sjdw.getLng().trim());
				String zb = gps.getWgLat()+","+gps.getWgLon();
				String path = geocodingAPI.getPath(zb, coordtype.wgs84ll);
				String yysPath = geocodingAPI.getPath(yysZb2, coordtype.wgs84ll);
				String yysGpsPath = geocodingAPI.getPath(yysGpsZb2, coordtype.wgs84ll);
				log.info(String.format("path=%s",path));
				log.info(String.format("yysPath=%s",yysPath));
				log.info(String.format("yysGpsPath=%s",yysGpsPath));

				if(path != null){
					//更新地址
					sjdw.setPath(path);
					sjdw.setYysPath(yysPath);
					sjdw.setYysGcjPath2(yysGpsPath);
					//更新 运营商GPS地址
					sjdw.setYysGcjLat2(String.valueOf(yysGpsZb.getWgLat()));
					sjdw.setYysGcjLng2(String.valueOf(yysGpsZb.getWgLon()));
					
					//计算误差
					double wc = DistanceUtil.getShortDistance(gps.getWgLat(),gps.getWgLon(), yysZb.getWgLat(), yysZb.getWgLon());//默认为GPS坐标
					double wc2 = DistanceUtil.getShortDistance(gps.getWgLat(),gps.getWgLon(), yysGpsZb.getWgLat(), yysGpsZb.getWgLon());//转成GPS坐标
					sjdw.setWcWgs(String.valueOf(wc));
					sjdw.setWcGcj(String.valueOf(wc2));
					
					//手机GPS度转换成二进制坐标
					sjdw.setLat2(String.valueOf(gps.getWgLat()));
					sjdw.setLng2(String.valueOf(gps.getWgLon()));
					dao.update(sjdw);
				}
			}
		}
	}
	
	/**
	 * 坐标转换
	 */
	public void zbzh(){
		Logger.getLogger(Wgs2Gcj.class);
		List<Object> objects = dao.findAll(Zbzh.class);
		
		List<Zbzh> zbzhs = new ArrayList<Zbzh>();
		for (Object object : objects) {
			if(object instanceof Zbzh){
				zbzhs.add((Zbzh)object);
			}
		}
		
		log.info("开始"+zbzhs.size()+"条记录");
		if(zhly == 1){
			//WGS  to  GCJ
			for (Zbzh zbzh : zbzhs) {
				if(zbzh.getWgsLat() != null && zbzh.getWgsLng() != null){
					Gps wgs = new Gps(Double.valueOf(zbzh.getWgsLat().trim()),Double.valueOf(zbzh.getWgsLng().trim()));
					Gps gcj = PositionUtil.gps84_To_Gcj02(wgs.getWgLat(), wgs.getWgLon());
					zbzh.setGcjLat(String.valueOf(gcj.getWgLat()));
					zbzh.setGcjLng(String.valueOf(gcj.getWgLon()));
					dao.update(zbzh);
					log.info(String.format("WGS转GCJ：wgs坐标%s  gcj坐标%s", wgs.getWgLat()+","+wgs.getWgLon(),gcj.getWgLat()+","+gcj.getWgLon()));
				}
			}
		}else if(zhly == 2){
			//GCJ to WGS
			for (Zbzh zbzh : zbzhs) {
				if(zbzh.getGcjLat() != null && zbzh.getGcjLng() != null){
					Gps gcj = new Gps(Double.valueOf(zbzh.getGcjLat().trim()),Double.valueOf(zbzh.getGcjLng().trim()));
					Gps gps = PositionUtil.gcj_To_Gps84(gcj.getWgLat(), gcj.getWgLon());
					zbzh.setWgsLat(String.valueOf(gps.getWgLat()));
					zbzh.setWgsLng(String.valueOf(gps.getWgLon()));
					dao.update(zbzh);
					log.info(String.format("GCJ转WGS：gcj坐标%s  wgs坐标%s", gcj.getWgLat()+","+gcj.getWgLon(),gps.getWgLat()+","+gps.getWgLon()));
				}
			}
		}
	}
	
	/**
	 * //分秒 转 二进
	 * @param lat
	 * @param lng
	 * @return
	 */
	public static Gps du2ejz(String lat,String lng){
		double du = Double.valueOf(lat.substring(0, 2));
		double mm = Double.valueOf(lat.substring(3, 5));
		double ss = Double.valueOf(lat.substring(5, 7)+"."+lat.substring(7, 10));
		lat = String.valueOf(du+mm/60+ss/3600);
		if(lat.length() > 9){
			lat = lat.substring(0, 9);
		}
		System.out.println(lat);
		
		double lng_du = Double.valueOf(lng.substring(0, 3));
		double lng_mm = Double.valueOf(lng.substring(4, 6));
		double lng_ss = Double.valueOf(lng.substring(6, 8)+"."+lng.substring(8, 11));
		lng = String.valueOf(lng_du+lng_mm/60+lng_ss/3600);
		if(lng.length()> 10){
			lng = lng.substring(0, 10);
		}
		System.out.println(lng);
		Gps gps = new Gps(Double.valueOf(lat),Double.valueOf(lng));
		return gps;
	}

}
